﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Linq;
using NuPattern;
using NuPattern.Diagnostics;
using NuPattern.Runtime.Validation;

namespace SearchFormToolkit.Automation.ValidationRules
{
    /// <summary>
    /// A custom validation rule that verifies values of properties or instances of elements.
    /// </summary>
    [DisplayName("Search Form Has Minimum Search Criteria")]
    [Category("MVC Search Form Automation")]
    [Description("Validates that the search form has at least the minimum number of search criteria defined.")]
    [CLSCompliant(false)]
    public class TextCriteriaRequiredRule : ValidationRule
    {
        private static readonly ITracer tracer = Tracer.Get<TextCriteriaRequiredRule>();

        /// <summary>
        /// Gets or sets the minimum number of search criteria required.
        /// </summary>
        [DisplayName("Min Criteria")]
        [Description("The minimum number of criteria required.")]
        [DesignOnly(true)]
        public string MinCriteria
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the current element.
        /// </summary>
        [Required]
        [Import(AllowDefault = true)]
        public ISearchForm CurrentElement
        {
            get;
            set;
        }

        /// <summary>
        /// Evaluates the violations for the rule.
        /// </summary>
        /// <remarks></remarks>
        public override IEnumerable<ValidationResult> Validate()
        {
            List<ValidationResult> errors = new List<ValidationResult>();

            this.ValidateObject();

            tracer.Info(
                "Validating minimum search criteria on current element '{0}' with MinCriteria '{1}'", this.CurrentElement.InstanceName, this.MinCriteria);

            var searchCriteria = this.CurrentElement.DefaultView.SearchCriteria;
            var criteriaCount = searchCriteria.DateCriterions.Count() +
                searchCriteria.MoneyCriterions.Count() +
                searchCriteria.NumberCriterions.Count() +
                searchCriteria.TextCriterions.Count();

            int minCriteria = 0;
            if (int.TryParse(this.MinCriteria, out minCriteria))
            {
                if (criteriaCount < minCriteria)
                {
                    errors.Add(new ValidationResult(
                        String.Format(CultureInfo.CurrentCulture,
                        "'{0}' must have at least '{1}' search criteria configured.", this.CurrentElement.InstanceName, this.MinCriteria)));
                }
            }

            tracer.Info(
                "Validated minimum search criteria on current element '{0}' with MinCriteria '{1}', as '{2}'", this.CurrentElement.InstanceName, this.MinCriteria, !errors.Any());

            return errors;
        }
    }
}
